package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Job;
import com.bosssoft.boss_exam_db.entity.MyGroup;
import com.bosssoft.boss_exam_db.mapper.GroupMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.CompanyService;
import com.bosssoft.boss_exam_db.service.busines.GroupService;
import com.bosssoft.boss_exam_db.service.busines.JobService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GroupServiceImpl extends BaseServiceImpl<MyGroup> implements GroupService {

    //注入GroupMapper
    @Autowired
    private GroupMapper groupMapper;

    //注入公司service
    @Autowired
    private CompanyService companyService;

    //注入岗位service
    @Autowired
    private JobService jobService;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/14
     * @Description
     * @Param [groupname, page, limit, sort, order] 集团名 、当前页数、每页条数、排序依据、排序方式
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.MyGroup>
     * @Exception 无
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<MyGroup> queryByCondition(String groupname, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(MyGroup.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(groupname)) {
            criteria.andLike("groupname", MySqlUtil.cloumLike(groupname));
        }
        criteria.andEqualTo("deleted",false);
        PageInfo<MyGroup> groupPageInfo = PageHelper.startPage(page, limit).setOrderBy(sort + " " + order).doSelectPageInfo(() ->
                this.groupMapper.selectByExample(example)
        );
        return groupPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/16
     * @Description  删除集团并删除集团下的所有公司
     * @Param [group] 删除的集团实例，可以只包含ID
     * @Return int 影响条数
     * @Exception 反射相关异常
     * @since JDK 1.8
     **/
    @Override
    public int deleteCascade(MyGroup group) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //删除集团
        int groupFlag = this.deleteForLogic(group);
        //删除公司
        Example companyExample = new Example(Company.class);
        Example.Criteria criteria = companyExample.createCriteria();
        criteria.andEqualTo("groupId",group.getId());
        Company company = new Company();
        company.setDeleted(true);
        int companyFlag = companyService.deleteForLogicByExample(company, companyExample);
        //删除岗位
        List<Company> companies = companyService.selectByExample(companyExample);
        List<String> companyIds = companies.stream().map(c -> c.getId() ).collect(Collectors.toList());
        Example jobExample = new Example(Job.class);
        Example.Criteria jobExampleCriteria = jobExample.createCriteria();
        jobExampleCriteria.andIn("companyId",companyIds);
        Job job = new Job();
        job.setDeleted(true);
        int jobFlag = jobService.deleteForLogicByExample(job, jobExample);
        //todo 考核科目以后要删
        if(groupFlag==1&&companyFlag>=0&&jobFlag>=0){
            return 1;
        }else {
            return 0;
        }
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/31
     * @Description 查询所有的集团
     * @Param []
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.MyGroup>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<MyGroup> queryAllGroup() {
        Example example = new Example(MyGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted",false);
        List<MyGroup> groups = this.groupMapper.selectByExample(example);
        return groups;
    }
}
